<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <meta name="description" content="Your description goes here" />
        <meta name="keywords" content="power control electricity meter" />
        <meta name="author" content="Benny Saxen" />
        <link rel="stylesheet" type="text/css" href="saxen.css" title="saxen" media="screen,projection" />

<?
$nameMonth[1]  = 'januari';
$nameMonth[2]  = 'februari';
$nameMonth[3]  = 'mars';
$nameMonth[4]  = 'april';
$nameMonth[5]  = 'maj';
$nameMonth[6]  = 'juni';
$nameMonth[7]  = 'juli';
$nameMonth[8]  = 'augusti';
$nameMonth[9]  = 'september';
$nameMonth[10] = 'oktober';
$nameMonth[11] = 'november';
$nameMonth[12] = 'december';

// Month size

$limMonth[1]  = 31;
$limMonth[2]  = 28;
$limMonth[3]  = 31;
$limMonth[4]  = 30;
$limMonth[5]  = 31;
$limMonth[6]  = 30;
$limMonth[7]  = 31;
$limMonth[8]  = 31;
$limMonth[9]  = 30;
$limMonth[10] = 31;
$limMonth[11] = 30;
$limMonth[12] = 31;

// Skottår
$limYear[2016][2] = 1;
$limYear[2020][2] = 1;
$limYear[2024][2] = 1;
$limYear[2028][2] = 1;

$idName[1111] = 'Lidback';
$idName[2222] = '-';
$idName[3333] = '-';
$idName[4444] = '-';
$idName[5555] = '-';
$idName[6666] = '-';
$idName[7777] = '-';
$idName[8888] = '-';
$idName[9999] = '-';
// Title

$thisFile = 'lidback.php';

echo("<title>$thistFile</title></head>");

echo("
<STYLE type=text/css>
A:link {COLOR: blue;text-decoration:none;}
A:visited {COLOR: blue}
A:hover {COLOR: green}
BODY {COLOR: black} 
</STYLE>
");

session_start(); 
$g_id     = $_SESSION['g_id'];

$g_width  = $_SESSION['g_width'];
$g_height = $_SESSION['g_height'];
$g_scale  = $_SESSION['g_scale'];
$g_ymax   = $_SESSION['g_ymax'];

$s_year   = $_SESSION['s_year'];
$s_month  = $_SESSION['s_month'];
$s_day    = $_SESSION['s_day'];

$temp                 = $_POST['g_id'];
if($temp)$g_id        = $_POST['g_id'];

$temp                 = $_POST['s_year'];
if($temp)$s_year      = $_POST['s_year'];
if(!$s_year)$s_year   = date('Y');

$temp                 = $_POST['s_month'];
if($temp)$s_month     = $_POST['s_month'];
if(!$s_month)$s_month = date('m');
$s_month += 0;

$temp                 = $_POST['s_day'];
if($temp)$s_day       = $_POST['s_day'];
if(!$s_day)$s_day     = date('d');
$s_day += 0;


$_SESSION['g_id']     = $g_id;

$_SESSION['s_year']   = $s_year;
$_SESSION['s_month']  = $s_month;
$_SESSION['s_day']    = $s_day;

//===================================================
// HTML
//===================================================

echo("<html><head><script src=\"processing-1.4.1.js\"></script>");

echo("</head><body>");

if($g_id > 0)
{
  $dateString = $s_year.'-';
  if($s_month < 10) $dateString = $dateString.'0';
  $dateString = $dateString.$s_month.'-';
  if($s_day < 10) $dateString = $dateString.'0';
  $dateString = $dateString.$s_day;

  $pdeFile = $g_id.'.pde';
  //echo("$dateString + $pdeFile<br>");

  $conf[id]     = $g_id;
  $conf[width]  = 1200;
  $conf[height] = 500;
  createPDE_single($pdeFile,$dateString,$conf);

  echo("<div style=\"margin-top:10px;\" id=upper1 align=left margin>");
  echo("<canvas data-processing-sources=\"$pdeFile\"></canvas>");
  echo("</div>");
}
else
{
  echo("<h2>No id selected</h2>");
}

  echo("<form action=\"$thistFile\" method=\"POST\">");
  echo("<input name=\"f_event\"  type=\"hidden\" value=\"event_day\"/>");
  echo("<select name=\"s_year\">");
  for($ii=2014;$ii<=2015;$ii++)
  {
    $selected = ' ';
    if($s_year == $ii)$selected = 'selected';
    echo("<option $selected value=\"$ii\">$ii</option>");
  }
  echo("</select>");
  echo("<select name=\"s_month\">");
  for($ii=1;$ii<=12;$ii++)
  {
    $selected = ' ';
    if($s_month == $ii)$selected = 'selected';
    echo("<option $selected value=\"$ii\">$nameMonth[$ii]</option>");
  }
  echo("</select>");

  echo("<select name=\"s_day\">");

  for($ii=1;$ii<=$limMonth[$s_month]+$limYear[$s_year][$s_month];$ii++)
  {
    $selected = ' ';
    if($s_day == $ii)$selected = 'selected';
       echo("<option $selected value=\"$ii\">$ii</option>");
  }
  echo("</select>");

  echo("<input type=\"submit\" value=\"v&auml;lj dag\" name=\"submit\">
  </form>");

//======================================================
  echo("<form action=\"$thistFile\" method=\"POST\">");
  echo("<input name=\"f_event\"  type=\"hidden\" value=\"unit_id\"/>");
  echo("<select name=\"g_id\">");
  for($ii=1;$ii<=9;$ii++)
  {
    $temp = $ii*1111;
    $name = $idName[$temp];
    $selected = ' ';
    if($g_id == $temp)$selected = 'selected';
    echo("<option $selected value=\"$temp\">$name</option>");
  }
  echo("</select>");
  echo("<input type=\"submit\" value=\"Unit Id\" name=\"submit\">
  </form>");
//============================
function createPDE_single($file,$log,$conf)
{
$f = fopen($file,'w'); 

$c_width  = $conf[width];
$c_height = $conf[height];
$c_id     = $conf[id];

$content = "String[] lines;
int i,temp,maxVal=0,scaleVal;
float ot=0,op=0;
int floor=0;
int xstart;
int xend;
float power=0.,t;
int h,m,s;

int maxValue()
{
  float maxPower=0;
  int temp=0;
  lines = loadStrings(\"data-$c_id-$log.bess\");

  for(i=0;i<lines.length;i++)
  {
     String s2 = trim(lines[i]);
     String[] pieces = splitTokens(s2, \" \");
     if (pieces.length == 4) 
     {
       h = int(pieces[0]);
       m = int(pieces[1]);
       s = int(pieces[2]);
       power = float(pieces[3]);
     }
     t = h*3600+m*60+s;
     if(t > xstart && t < xend)
     {
       if(maxPower < power)maxPower = power;
     }
  }
  for(i=0;i<100;i++)
  {
     if(maxPower > i*1000.0) temp = (i+1)*1000; 
  }
  return(temp);
}


int getScale(int value)
{
  int res = 50;
  if(value > 500)   res = 100;
  if(value > 1000)  res = 200;
  if(value > 2000)  res = 250;
  if(value > 4000)  res = 500;
  if(value > 10000) res = 1000;
  if(value > 20000) res = 2000;
  if(value > 50000) res = 10000;
  return(res);
}

void setup() {
  xstart = 0;
  xend   = 24*3600;
  size($c_width, $c_height);
  background(240);
  fill(255,0,0);
  //text('$log',10,10);
  stroke(150,150,150);
  //line(0,$c_height-1,$c_width,$c_height-1);

   for(i=1;i<24;i++)
   {
    temp = int(i/24.*$c_width);
    line(temp,$c_height,temp,15);
    text(i,temp,20);
   }

  maxVal = maxValue();
  scaleVal = getScale(maxVal);

  for(i=1;i<=int(maxVal/scaleVal);i++)
  {
    float yp = float(i*scaleVal)/(maxVal/$c_height);
    yp=$c_height-yp;
    line(0,yp,$c_width,yp);
    text(i*scaleVal,0,yp);
  }

  stroke(0,0,0);
  lines = loadStrings(\"data-$c_id-$log.bess\");
  int j,k,from,to;
  float oldP=0,t_prev=0,p=0;

  //for(i=0;i<=$c_filter;i++)phist[i] = 0.;
  for(i=0;i<lines.length;i++)
  {
     String s2 = trim(lines[i]);
     String[] pieces = splitTokens(s2, \" \");
     if (pieces.length == 4) 
     {
       h = int(pieces[0]);
       m = int(pieces[1]);
       s = int(pieces[2]);
       power = float(pieces[3]);
     }

     t = h*3600+m*60+s;


       if(t > xstart && t < xend)
       {
        // presentation 
        float p1 = power/(maxVal/$c_height);    
        float x = (t-xstart)/(xend - xstart)*$c_width;
        p1=$c_height-p1;
        float y = p1;
        line(ot,op,x,y);
        ot=x;op=y;
       }
     
  }
}


void draw() 
{
}

";
fwrite($f,$content,strlen($content)); 
fclose($f); 
}
